home *** CD-ROM | disk | FTP | other *** search
/ Tech Arsenal 1 / Tech Arsenal (Arsenal Computer).ISO / tek-01 / ohlutil.zip / MKFIFO.C < prev    next >
C/C++ Source or Header  |  1990-06-22  |  5KB  |  214 lines

  1. /* mkfifo -- make fifo's (named pipes)
  2.    Copyright (C) 1990 Free Software Foundation, Inc.
  3.  
  4.    This program is free software; you can redistribute it and/or modify
  5.    it under the terms of the GNU General Public License as published by
  6.    the Free Software Foundation; either version 1, or (at your option)
  7.    any later version.
  8.  
  9.    This program is distributed in the hope that it will be useful,
  10.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  11.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12.    GNU General Public License for more details.
  13.  
  14.    You should have received a copy of the GNU General Public License
  15.    along with this program; if not, write to the Free Software
  16.    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */
  17.  
  18. /* Usage: mkfifo [-p] [-m mode] [+path] [+mode mode] path...
  19.  
  20.    Options:
  21.    -p, +path        Ensure that the given path(s) exist:
  22.             Make any missing parent directories for each argument.
  23.             Parent dirs default to umask modified by `u+wx'.
  24.             Do not consider an argument fifo that already
  25.             exists to be an error.
  26.    -m, +mode mode    Set the mode of created fifo's to `mode', which is
  27.             symbolic as in chmod and uses the umask as a point of
  28.             departure.
  29.  
  30.    David MacKenzie <djm@ai.mit.edu>  */
  31.  
  32. #include <stdio.h>
  33. #include <sys/types.h>
  34. #include "system.h"
  35. #include "getopt.h"
  36. #include "modechange.h"
  37.  
  38. #ifdef STDC_HEADERS
  39. #include <errno.h>
  40. #include <stdlib.h>
  41. #else
  42. extern int errno;
  43. #endif
  44.  
  45. #ifndef _POSIX_SOURCE
  46. #define mkfifo(path, mode) (mknod ((path), (mode) | S_IFIFO, 0))
  47. #endif
  48.  
  49. int make_path ();
  50. void error ();
  51. void strip_trailing_slashes ();
  52. void usage ();
  53.  
  54. /* If nonzero, ensure that a path exists.  */
  55. int path_mode;
  56.  
  57. /* The name this program was run with. */
  58. char *program_name;
  59.  
  60. struct option longopts[] =
  61. {
  62.   {"mode", 1, NULL, 'm'},
  63.   {"path", 0, &path_mode, 1},
  64.   {NULL, 0, NULL, 0}
  65. };
  66.  
  67. void
  68. main (argc, argv)
  69.      int argc;
  70.      char **argv;
  71. {
  72.   unsigned short newmode;
  73.   unsigned short parent_mode;
  74.   struct mode_change *change;
  75.   char *symbolic_mode;
  76.   int errors = 0;
  77.   int optc;
  78.   int ind;
  79.  
  80.   program_name = argv[0];
  81.   path_mode = 0;
  82.   symbolic_mode = NULL;
  83.  
  84.   while ((optc = getopt_long (argc, argv, "pm:", longopts, &ind)) != EOF)
  85.     {
  86.       if (optc == 0 && longopts[ind].flag == 0)
  87.     optc = longopts[ind].val;
  88.       switch (optc)
  89.     {
  90.     case 0:            /* Long option. */
  91.       break;
  92.     case 'p':
  93.       path_mode = 1;
  94.       break;
  95.     case 'm':
  96.       symbolic_mode = optarg;
  97.       break;
  98.     default:
  99.       usage ();
  100.     }
  101.     }
  102.  
  103.   if (optind == argc)
  104.     usage ();
  105.   
  106.   newmode = 0777 & ~umask (0);
  107.   parent_mode = newmode | 0300;    /* u+wx */
  108.   newmode &= ~0111;        /* Remove execute permission. */
  109.   if (symbolic_mode)
  110.     {
  111.       change = mode_compile (symbolic_mode, MODE_MASK_EQUALS | MODE_MASK_PLUS);
  112.       if (change == MODE_INVALID)
  113.     error (1, 0, "invalid mode");
  114.       else if (change == MODE_MEMORY_EXHAUSTED)
  115.     error (1, 0, "virtual memory exhausted");
  116.       newmode = mode_adjust (newmode, change);
  117.     }
  118.  
  119.   for (; optind < argc; ++optind)
  120.     {
  121.       strip_trailing_slashes (argv[optind]);
  122.       if (path_mode)
  123.     errors |= make_path (argv[optind], newmode, parent_mode);
  124.       else if (mkfifo (argv[optind], newmode))
  125.     {
  126.       error (0, errno, "cannot make fifo `%s'", argv[optind]);
  127.       errors = 1;
  128.     }
  129.     }
  130.  
  131.   exit (errors);
  132. }
  133.  
  134. /* Make sure fifo `path' and all leading directories exist,
  135.    and give it permission mode `mode'.
  136.    If any leading directories are created, give them permission
  137.    mode `parent_mode'.
  138.    Return 0 if successful, 1 if errors occur. */
  139.  
  140. int
  141. make_path (path, mode, parent_mode)
  142.      char *path;
  143.      unsigned short mode;
  144.      unsigned short parent_mode;
  145. {
  146.   char *slash;
  147.   struct stat stats;
  148.  
  149.   if (stat (path, &stats))
  150.     {
  151.       slash = path;
  152.       while (*slash == '/')
  153.     slash++;
  154.       while (slash = index (slash, '/'))
  155.     {
  156.       *slash = 0;
  157.       if (stat (path, &stats))
  158.         {
  159.           if (mkdir (path, parent_mode))
  160.         {
  161.           error (0, errno, "cannot make directory `%s'", path);
  162.           return 1;
  163.         }
  164.         }
  165.       else if ((stats.st_mode & S_IFMT) != S_IFDIR)
  166.         {
  167.           error (0, 0, "`%s' is not a directory", path);
  168.           return 1;
  169.         }
  170.       *slash++ = '/';
  171.     }
  172.  
  173.       if (mkfifo (path, mode))
  174.     {
  175.       error (0, errno, "cannot make fifo `%s'", path);
  176.       return 1;
  177.     }
  178.     }
  179.   else if ((stats.st_mode & S_IFMT) != S_IFIFO)
  180.     {
  181.       error (0, 0, "`%s' is not a fifo", path);
  182.       return 1;
  183.     }
  184.   else if (chmod (path, mode))
  185.     {
  186.       error (0, errno, "cannot change mode of `%s'", path);
  187.       return 1;
  188.     }
  189.   return 0;
  190. }
  191.  
  192. /* Remove trailing slashes from PATH; they cause some system calls to fail. */
  193.  
  194. void
  195. strip_trailing_slashes (path)
  196.      char *path;
  197. {
  198.   int last;
  199.  
  200.   last = strlen (path) - 1;
  201.   while (last > 0 && path[last] == '/')
  202.     path[last--] = '\0';
  203. }
  204.  
  205. void
  206. usage ()
  207. {
  208.   fprintf (stderr, "\
  209. Usage: %s [-p] [-m mode] [+path] [+mode mode] path...\n",
  210.        program_name);
  211.   exit (1);
  212. }
  213.  
  214.